package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.common.result.PageResult;
import com.quwan.config.yml.OssConfig;
import com.quwan.entity.TMemberAsk;
import com.quwan.manager.service.MemberAskService;
import com.quwan.mapper.TMemberAskMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author laozhang
 * @date 2021/11/18 9:36
 */
@Service
@RequiredArgsConstructor
public class MemberAskServiceImpl implements MemberAskService {
    private final TMemberAskMapper memberAskMapper;
    private final OssConfig ossConfig;
    @Override
    public PageResult<TMemberAsk> getMemberAskListPage(int page, int size, Integer status, String name, String beginTime, String endTime, String mId) {
        LambdaQueryWrapperX<TMemberAsk> wrapper = new LambdaQueryWrapperX<TMemberAsk>()
                .orderByDesc(TMemberAsk::getAddTime);
        if(StringUtils.isNotBlank(name)) {
            wrapper.like(TMemberAsk::getName,name);
        }
        if(StringUtils.isNotEmpty(mId)){
            wrapper.eq(TMemberAsk::getMId, mId);
        }
        if(status !=null) {
            wrapper.eq(TMemberAsk::getStatus,status);
        }
        if(StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(TMemberAsk::getAddTime,beginTime);
        }
        if(StringUtils.isNotBlank(endTime)) {
            wrapper.le(TMemberAsk::getAddTime,endTime+" 23:59:59");
        }

        Page<TMemberAsk> memberAskPage = memberAskMapper.selectPage(new Page<>(page, size), wrapper);
        List<TMemberAsk> reslist = memberAskPage.getRecords();
        /* 开始遍历 */
        for (TMemberAsk ban : reslist) {
            if(ban.getImg()!= null && ban.getImg().length()>0){
                ban.setImg(ossConfig.getHostname()+"/"+ban.getImg());
            }
        }
        PageResult<TMemberAsk> result = new PageResult<TMemberAsk>()
                .setCurrent(memberAskPage.getCurrent())
                .setPages(memberAskPage.getPages())
                .setSize(memberAskPage.getSize())
                .setTotal(memberAskPage.getTotal())
                .setRecords(reslist);
        return result;
    }

    @Override
    public List<TMemberAsk> getMemberAskList(Integer status, String name, String beginTime, String endTime) {
        LambdaQueryWrapperX<TMemberAsk> wrapper = new LambdaQueryWrapperX<TMemberAsk>()
                .orderByDesc(TMemberAsk::getAddTime);
        if(StringUtils.isNotBlank(name)) {
            wrapper.like(TMemberAsk::getName,name);
        }
        if(status !=null) {
            wrapper.eq(TMemberAsk::getStatus,status);
        }
        if(StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(TMemberAsk::getAddTime,beginTime);
        }
        if(StringUtils.isNotBlank(endTime)) {
            wrapper.le(TMemberAsk::getAddTime,endTime+" 23:59:59");
        }
        List<TMemberAsk> reslist = memberAskMapper.selectList(wrapper);
        /* 开始遍历 */
        for (TMemberAsk ban : reslist) {
            if(ban.getImg()!= null && ban.getImg().length()>0){
                ban.setImg(ossConfig.getHostname()+"/"+ban.getImg());
            }
        }
        return reslist;
    }

    @Override
    public boolean updateMemberAskStatus(TMemberAsk memberAsk) {
        return memberAskMapper.updateById(memberAsk)==1;
    }
}
